using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Excel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(((univNETLib)Session[modzGeneral.mUsu_codigo + "NetLib"]).Idioma);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(((univNETLib)Session[modzGeneral.mUsu_codigo + "NetLib"]).Idioma);

        //ExportDocument(this, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]), Session[modzGeneral.mUsu_codigo + "excelFileName"].ToString(), "xls");
        //return;

        ((univNETLib)Session[modzGeneral.mUsu_codigo + "NetLib"]).m_DataTable = null;

        GridView grdExcel = new GridView();

        grdExcel.AutoGenerateColumns = false;
        Table tbl = new Table();

        grdExcel.AllowSorting = false;

        GridViewRow hr = new GridViewRow(((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.RowIndex, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.DataItemIndex, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.RowType, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.RowState);
        for (int ncell = 0; ncell <= ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells.Count - 1; ncell++)
        {
            if (((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].Controls.Count > 0 && (((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].Controls[0].GetType().Name == "Image" || ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].Controls[0].GetType().Name == "HtmlInputCheckBox"))
            {
                ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].Controls.RemoveAt(0);
            }
            ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].BackColor = System.Drawing.Color.Silver;
            ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].Text = ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow.Cells[ncell].Text.Replace("&lt;/br&gt;", " ").Replace("&lt;/Br&gt;", " ").Replace("&lt;/BR&gt;", " ");
        }
        hr = ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).HeaderRow;

        tbl.Controls.Add(hr);
        grdExcel.Controls.Add(tbl);
        
        foreach (GridViewRow rw in ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).Rows)
        {
            rw.Style.Clear();
            if (rw.RowType == DataControlRowType.DataRow)
            {
                GridViewRow nr = new GridViewRow(rw.RowIndex, rw.DataItemIndex, rw.RowType, rw.RowState);
                nr = rw;

                if (nr.Cells[nr.Cells.Count - 1].BackColor == System.Drawing.ColorTranslator.FromHtml("#FFE3A4"))
                {
                    for (int ncell = 0; ncell <= (nr.Cells.Count - 1); ncell++)
                    {
                        nr.Cells[ncell].BackColor = System.Drawing.Color.White;
                    }
                }
                if (rw.RowIndex >= 1000)
                {
                    GridViewRow nrf = new GridViewRow(1002, 1002, nr.RowType, nr.RowState);  
                    nrf.Cells.Add(new TableCell());
                    nrf.Cells[0].Text = GlobalTranslate.translateTermo("Para mais que 1000 linhas utilize a Tarefa 'Export (Data Only)'");
                    nrf.Cells[0].Visible = true;
                    nrf.Cells[0].BackColor = System.Drawing.Color.Maroon;
                    nrf.Cells[0].ForeColor = System.Drawing.Color.White;
                    nrf.Cells[0].Font.Bold = true; 
                    tbl.Controls.Add(nrf);
                    grdExcel.Controls.Add(tbl);
                    ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).AllowPaging = true;
                    ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).DataBind();
                    break;
                }

                tbl.Controls.Add(nr);
                grdExcel.Controls.Add(tbl);
            }
        }
        

        GridViewRow fr = new GridViewRow(((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.RowIndex, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.DataItemIndex, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.RowType, ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.RowState);
        fr.Visible = true;
        for (int ncell = 0; ncell <= ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.Cells.Count - 1; ncell++)
        {
            if (((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.Cells[ncell].Controls.Count > 0 && ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.Cells[ncell].Controls[0].GetType().Name == "HtmlImage")
            {
                ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow.Cells[ncell].Controls.RemoveAt(0);
                fr.Visible = false;
            }
        }
        fr = ((GridView)Session[modzGeneral.mUsu_codigo + "grdExcel"]).FooterRow;
        tbl.Controls.Add(fr);
        grdExcel.Controls.Add(tbl);

        string attachment = "attachment; filename=" + Session[modzGeneral.mUsu_codigo + "excelFileName"] + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grdExcel.RenderControl(htw);
        Response.Write(sw.ToString());
        //grdExcel.DataBind(); 
        Session.Remove(modzGeneral.mUsu_codigo + "grdExcel");
        grdExcel = null;
        Response.End();

    }

    public static void ExportDocument(Page pagina, GridView grid, String nomeArquivo, String contentType)
    {
        grid.DataBind();
        pagina.Response.Clear();
        pagina.Response.Charset = "";
        pagina.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        pagina.EnableViewState = false;
        grid.EnableViewState = false;

        string attachment = "attachment; filename=" + nomeArquivo + ".xls";
        pagina.Response.ClearContent();
        pagina.Response.AddHeader("content-disposition", attachment);
        pagina.Response.ContentType = "application/vnd.xls";

        pagina.Form.Controls.Add(grid);

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        pagina.Form.RenderControl(htw);
        pagina.Response.Write(sw.ToString());

        grid.AllowPaging = true;

        grid.DataBind();

        pagina.Response.End();

        return;
    }




}
